package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.HomeGoods;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyrecyclerview.adapter.BaseViewHolder;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class HomeShopAdapter extends RecyclerArrayAdapter<HomeGoods.DataBean> {
    private Context mContext;

    public HomeShopAdapter(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

        return new CouponViewHolder(parent);
    }

    public class CouponViewHolder extends BaseViewHolder<HomeGoods.DataBean> {
        TextView tv_money, tv_title, tv_spec, tv_place, tv_stock, tv_unit_price, tv_single_spec;
        ImageView iv_banner, iv_expiring;

        public CouponViewHolder(ViewGroup parent) {
            super(parent, R.layout.shop_item);
            tv_money = $(R.id.tv_money);
            tv_title = $(R.id.tv_title);
            tv_spec = $(R.id.tv_spec);
            tv_place = $(R.id.tv_place);
            tv_stock = $(R.id.tv_stock);
            tv_unit_price = $(R.id.tv_unit_price);
            iv_banner = $(R.id.iv_banner);
            iv_expiring = $(R.id.iv_expiring);
            tv_single_spec = $(R.id.tv_single_spec);
        }

        @Override
        public void setData(final HomeGoods.DataBean bean, int position) {
            String thumbPath = bean.getThumb();
            int is_expiring = bean.getIs_expiring();
            String goods_name = bean.getGoods_name();
            String specifications = bean.getSpecifications();
            String num1 = bean.getNum();
            int num = 0;
            if (!Tools.isNull(num1)) {
                num = Integer.parseInt(num1);
            }
            String market_price = bean.getMarket_price();
            String country_name = bean.getCountry_name();
            String total_stock = bean.getTotal_stock();
            String packageX = bean.getPackageX();
            String single_specifications = bean.getSingle_specifications();
            String single_price = bean.getSingle_price();
            String price = bean.getPrice();
            String single_num = bean.getSingle_num();
            String unit = "";
            if (!Tools.isNull(single_specifications)) {
                unit = single_specifications;
            } else {
                unit = "";
            }
            if (!Tools.isNull(single_specifications)) {
                tv_single_spec.setText("单品规格：" + single_num + unit);
            } else {
                tv_single_spec.setText("");
            }

            if (!Tools.isNull(single_price)) {
                tv_unit_price.setText("￥" + single_price);
            } else {
                tv_unit_price.setText("￥0");
            }
            if (!Tools.isNull(thumbPath)) {
                String path = "";
                if (thumbPath.startsWith("http")) {
                    path = thumbPath;
                } else {
                    path = HttpUtil.IMG_THUMB_BASE_URL + thumbPath;
                }
                Glide.with(mContext)
                        .load(path)
                        .centerCrop()
                        .placeholder(R.mipmap.loading_4b3)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(iv_banner);
            } else {
                iv_banner.setImageResource(R.mipmap.loading_4b3);
            }
            if (!Tools.isNull(goods_name)) {
                tv_title.setText(goods_name);
            } else {
                tv_title.setText("商品暂无内容");
            }

            if (!Tools.isNull(num)) {
                tv_spec.setText("规格：" + num + specifications + "/" + packageX);
            } else {
                tv_spec.setText("规格：");
            }

            if (!Tools.isNull(price)) {
                tv_money.setText("￥" + price + "/" + packageX);
            } else {
                tv_money.setText("￥0件");
            }

            if (!Tools.isNull(country_name)) {
                tv_place.setText("产地：" + country_name);
            } else {
                tv_place.setText("产地：不详");
            }

            if (!Tools.isNull(total_stock)) {
                tv_stock.setText("库存：" + total_stock);
            } else {
                tv_stock.setText("库存：0件");
            }
            switch (is_expiring) {
                case 1:
                    iv_expiring.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    iv_expiring.setVisibility(View.INVISIBLE);
                    break;
            }
        }
    }
}
